package com.xia.service.impl;

import com.xia.dao.article.IArticleDao;
import com.xia.dao.article.ICollectDao;
import com.xia.dao.article.ILikeArticleDao;
import com.xia.dao.users.IAttentionDao;
import com.xia.dao.users.IUsersDao;
import com.xia.domian.*;
import com.xia.service.IAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IAttentionServiceImpl implements IAttentionService {
    private IAttentionDao attentionDao;
    private IUsersDao usersDao;
    private IArticleDao articleDao;
    private ICollectDao collectDao;
    private ILikeArticleDao likeArticleDao;

    @Autowired
    public void setLikeArticleDao(ILikeArticleDao likeArticleDao) {
        this.likeArticleDao = likeArticleDao;
    }
    @Autowired
    public void setCollectDao(ICollectDao collectDao) {
        this.collectDao = collectDao;
    }
    @Autowired
    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }
    @Autowired
    public void setUsersDao(IUsersDao usersDao) {
        this.usersDao = usersDao;
    }
    @Autowired
    public void setAttentionDao(IAttentionDao attentionDao) {
        this.attentionDao = attentionDao;
    }

    @Override
    public Map<String, Object> selectUsersInCreator(Integer usersId) {
        Map<String,Object> map=new HashMap<>();
        Integer pageView=0;
        Integer like=0;
        List<Article> pageViewAndLikeList=articleDao.selectArticlePageViewAndLike(usersId);
        for (Article articlePageViewAndLike : pageViewAndLikeList) {
            pageView+=articlePageViewAndLike.getArticlePageView();
            like+=articlePageViewAndLike.getArticleLike();
        }
        List<Integer> attentionNum=attentionDao.selectUsersIsFollow(usersId);
        Integer collectNum=collectDao.selectUsersCollect(usersId);
        map.put("collectNum",collectNum);
        map.put("attentionNum",attentionNum.size());
        map.put("pageView",pageView);
        map.put("like",like);
        return map;
    }

    @Override
    public List<Article> selectUsersDynamic(Integer usersId,Integer index) {
        List<Integer> attentionUsersId=attentionDao.selectUsersIsAttention(usersId);
        return selectDynamicAllUtil(attentionUsersId,index);
    }

    @Override
    public List<Article> selectDynamicAllUtil(List<Integer> attentionUsersId,Integer index){
        List<Article> articleList=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        //被关注用户ID存入map
        map.put("condition",attentionUsersId);
        map.put("index",index);
        List<Article> sortArticleList;
        try {
            sortArticleList=articleDao.selectDynamicSort(map);
        }catch (Exception e){
            return null;
        }
        for (Article articleUtil : sortArticleList) {
            if (articleUtil.getArticleFromState() == 1){
                List<Article> list = articleDao.selectArticleOnId(0,articleUtil.getArticleId(), 0, 0);
                list.get(0).setDynamicIdentification(1);
                articleList.add(list.get(0));
            }else if (articleUtil.getArticleFromState() == 2){
                Collect collect=collectDao.selectCollectById(articleUtil.getArticleId());
                List<Article> list = articleDao.selectArticleOnId(0,collect.getCollectArticle(), 0, 0);
                for (Article article : list) {
                    Users users=usersDao.selectUsersById(collect.getCollectUsers());
                    article.getUsers().setUsersId(users.getUsersId());
                    article.getUsers().setUsersName(users.getUsersName());
                    article.getUsers().setUsersPortrait(users.getUsersPortrait());
                    article.setArticleCreatingTime(articleUtil.getArticleCreatingTime());
                    article.setDynamicIdentification(3);
                    articleList.add(article);
                }
            }else if(articleUtil.getArticleFromState() == 3){
                LikeArticle likeArticle=likeArticleDao.selectLikeArticleById(articleUtil.getArticleId());
                List<Article> list = articleDao.selectArticleOnId(0,likeArticle.getLikeArticle(), 0, 0);
                for (Article article : list) {
                    Users users=usersDao.selectUsersById(likeArticle.getLikeUsers());
                    article.getUsers().setUsersId(users.getUsersId());
                    article.getUsers().setUsersName(users.getUsersName());
                    article.getUsers().setUsersPortrait(users.getUsersPortrait());
                    //将喜欢表中的创建时间替换文章的创建时间
                    article.setArticleCreatingTime(articleUtil.getArticleCreatingTime());
                    article.setDynamicIdentification(2);
                    articleList.add(article);
                }
            }
        }
        return articleList;
    }

    @Override
    public Attention selectAttention(Integer articleUsersId, Integer usersId) {
        List<Integer> followNum=attentionDao.selectUsersIsFollow(articleUsersId);
        List<Integer> attentionNum=attentionDao.selectUsersIsAttention(articleUsersId);
        return multipleAttentionSet(articleUsersId,followNum.size(),attentionNum.size(),followNum,usersId);
    }
    public Attention multipleAttentionSet(Integer usersId,Integer followNum,Integer attentionNum,List<Integer> forAttentionList,Integer conditionUser){
        Users users=usersDao.selectUsersById(usersId);
        int attentionState=0;
        boolean numState=forAttentionList.contains(conditionUser);
        if (numState){
            attentionState=1;
        }
        Attention attention = new Attention();
        attention.setUsersId(users.getUsersId());
        attention.setUsersFirstImage(users.getUsersFirstImage());
        attention.setUsersName(users.getUsersName());
        attention.setUsersPortrait(users.getUsersPortrait());
        attention.setAttentionNum(attentionNum);
        attention.setFollowNum(followNum);
        attention.setAttentionState(attentionState);
        return attention;
    }

    @Override
    public void updateAttention(Integer usersId, Integer followUsersId, Integer attentionState) {
        Integer attentionId=attentionDao.selectAttentionId(usersId,followUsersId);
        if (attentionId == null){
            Attention attention = new Attention();
            attention.setAttentionUsers(followUsersId);
            attention.setAttentionByUsers(usersId);
            attentionDao.insertAttention(attention);
            return;
        }
        attentionDao.updateAttention(attentionState,attentionId);
    }

    @Override
    public List<Attention> selectMsgAttention(Integer articleUsersId, Integer usersId) {
        List<Attention> multipleAttentionList=new ArrayList<>();
        List<Integer> articleUsersAttentionList=attentionDao.selectUsersIsAttention(articleUsersId);
        List<Integer> usersAttentionList=attentionDao.selectUsersIsAttention(usersId);
        List<Integer> sameList=new ArrayList<>();
        for (Integer value : usersAttentionList) {
            for (Integer integer : articleUsersAttentionList) {
                if (value.equals(integer)) {
                    sameList.add(value);
                }
            }
        }
        for (Integer integer : articleUsersAttentionList) {
            multipleAttentionList.add(multipleAttentionSet(integer,0,0,sameList,integer));
        }
        return multipleAttentionList;
    }
}
